package com.zlt.bkparking.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.CouponDTO;
import com.zlt.bkparking.entity.Coupon;
import com.zlt.bkparking.entity.CouponExample;
import com.zlt.bkparking.entity.ParkingLot;
import com.zlt.bkparking.entity.ParkingLotExample;
import com.zlt.bkparking.mapper.CouponMapper;
import com.zlt.bkparking.mapper.ParkingLotMapper;
import com.zlt.bkparking.service.CouponService;
import com.zlt.bkparking.utils.StringUtil;
import com.zlt.bkparking.vo.ActiveBackUser;
import com.zlt.bkparking.vo.CouponVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname CouponServiceImpl
 * @Description TODO
 * @Date 2022/10/5 13:20
 * @Created by ${Lijijun}
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private ParkingLotMapper parkingLotMapper;
    @Override
    public boolean insertCoupon(CouponDTO couponDTO) {
        Coupon coupon =new Coupon();
        BeanUtils.copyProperties(couponDTO,coupon);
        System.out.println("Coupon:"+coupon);

        if (!StringUtil.isNotNull(coupon.getcName()))
            throw new RuntimeException("优惠券名称为空");
        if (coupon.getcTotal()==null)
            throw new RuntimeException("优惠券数量不能为空");
        if (coupon.getcDiscount()==null)
            throw new RuntimeException("优惠金额不能为空");
        if (coupon.getcMin()==null)
            throw new RuntimeException("最少消费金额不能为空");
        if (coupon.getcStartTime()==null)
            throw new RuntimeException("优惠券开始时间不能为空");
        if (coupon.getcEndTime()==null)
            throw new RuntimeException("优惠券结束时间不能为空");
        if (coupon.getcLimit()==null)
            throw new RuntimeException("领取限制数量不能为空");
        if (coupon.getcStatus()==null)
            throw new RuntimeException("优惠券状态不能为空");

        //获取当前停车场管理员的bu_id  1
        ActiveBackUser subject = (ActiveBackUser) SecurityUtils.getSubject().getPrincipal();
        Long buId = subject.getBuId();
        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.createCriteria().andBuIdEqualTo(buId).andPlStateEqualTo((byte)0);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if (parkingLots==null||parkingLots.isEmpty())
            throw new RuntimeException("您没有管理的停车场,不能添加优惠券");
        Integer plId = parkingLots.get(0).getPlId();
        coupon.setPlId(plId);
        coupon.setcCreateTime(new Date());
        coupon.setUpdateTime(new Date());
        int i = couponMapper.insertSelective(coupon);
        return i>0;
    }

    @Override
    public PageInfo<CouponVO> selectCoupons(CouponDTO couponDTO) {
        Integer page = couponDTO.getPage();
        if (page==null)
            page=1;
        Integer limit = couponDTO.getLimit();
        if (limit==null)
            limit=5;
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponDTO,coupon);
        //获取当前停车场管理员的id
        ActiveBackUser subject = (ActiveBackUser) SecurityUtils.getSubject().getPrincipal();
        Long buId = subject.getBuId();

        ParkingLotExample parkingLotExample = new ParkingLotExample();
        parkingLotExample.createCriteria().andBuIdEqualTo(buId).andPlStateEqualTo((byte)0);
        List<ParkingLot> parkingLots = parkingLotMapper.selectByExample(parkingLotExample);
        if (parkingLots==null||parkingLots.isEmpty())
            throw new RuntimeException("您没有管理的停车场,无法查询优惠券");
        Integer plId = parkingLots.get(0).getPlId();
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        criteria.andPlIdEqualTo(plId);
        criteria.andIsDeletedEqualTo((byte)0);
        if (StringUtil.isNotNull(coupon.getcName()))
            criteria.andCNameLike("%"+coupon.getcName()+"%");
        if (coupon.getcStatus()!=null)
            criteria.andCStatusEqualTo(coupon.getcStatus());
        if (coupon.getcStartTime()!=null)
            criteria.andCStartTimeGreaterThanOrEqualTo(coupon.getcStartTime());
        PageHelper.startPage(page,limit);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<CouponVO> couponVOPageInfo = new PageInfo(coupons);
        List<CouponVO> couponVOList = new ArrayList<>();
        couponVOPageInfo.setList(couponVOList);
        if (coupons!=null && !coupons.isEmpty()){
            for (Coupon coupon1 : coupons) {
                CouponVO couponVO = new CouponVO();
                couponVOList.add(couponVO);
                BeanUtils.copyProperties(coupon1,couponVO);
            }
        }
        return couponVOPageInfo;
    }

    @Override
    public Boolean updateCoupon(CouponDTO couponDTO) {
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(couponDTO,coupon);
        if (coupon.getcId()==null)
            throw new RuntimeException("优惠券信息更新失败---主键不能为空");
        coupon.setUpdateTime(new Date());
        int i = couponMapper.updateByPrimaryKeySelective(coupon);
        return i>0;
    }
}
